﻿using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors;

namespace Awsc.DataAcquisitionSys.Core.Service.DataAcquisition.DataCollectors
{
    public class PL1000ECollector : ModbusTcpDataCollector
    {
        const ushort startAddress = 0;    //起始地址
        const ushort numberOfPoints = 30; //读数据长度

        public PL1000ECollector(DataCollectionContext dataCollectionContext) : base(dataCollectionContext)
        {
        }

        protected async override Task CollectDataInternalAsync()
        {
            ushort[] AIValue = ModbusMaster.ReadHoldingRegisters(CollectorConfig.SlaveAddress, startAddress, numberOfPoints);
            if (AIValue != null)
            {
                if (CollectorConfig.AICount <= 0)
                {
                    return;
                }
                //AI量
                //发动机冷却液温度 摄氏度转华氏度
                //values_AI[0] = 32.0f + AIValue[4] * 1.8f;
                var data1 = AIValue[4];
                SetAISensorValue(1, data1);

                //发动机机油压力 kPa
                var data2 = AIValue[10] * 0.5f * 0.145f;
                SetAISensorValue(2, data2);

                //发动机负荷系数 %
                var data3 = AIValue[12];
                SetAISensorValue(3, data3);

                //发动机转速 转/分
                var data4 = AIValue[13] * 0.5f;
                SetAISensorValue(4, data4);

                //诊断时钟  小时
                var data5 = AIValue[19];
                SetAISensorValue(5, data5);

                //蓄电池电压 伏特
                var data6 = AIValue[21] * 0.5f;
                SetAISensorValue(6, data6);

                //总燃油(消耗量) 升
                var data7 = AIValue[22] * 0.125f * 3.785411784f;
                SetAISensorValue(7, data7);

                //燃油压力 kPa
                var data8 = AIValue[26] * 0.5f * 0.145f;
                SetAISensorValue(8, data8);

                //燃油消耗量  L/h
                var data9 = AIValue[28] * 0.05f;
                SetAISensorValue(9, data9);


                //DI量
                //发动机低油压警告
                var coil1 = AIValue[0] != 0;
                SetDICoilValue(1, coil1);

                //发动机冷却液高温报警
                var coil2 = AIValue[6] != 0;
                SetDICoilValue(2, coil2);

                //发动机超速停机
                var coil3 = AIValue[15] != 0;
                SetDICoilValue(3, coil3);
            }
            else
            {

            }
        }
    }
}
